#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QSettings>
#include <QTextCodec>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
//    tld_run();
    mythr = new myThread();
//    mythr->start();
    QFileInfo fileInfo(QCoreApplication::applicationDirPath()+"/test.ini");
//    fileInfo.setFile(QCoreApplication::applicationDirPath()+"/test.ini");
    if (!fileInfo.isFile())
    {
        QSettings iniFile(QCoreApplication::applicationDirPath()+"/test.ini", QSettings::IniFormat);

        iniFile.setIniCodec(QTextCodec::codecForName("GB2312"));    //解决读取value值的中文乱码问题

        QString tempStr = QString("FileSet");
        iniFile.beginGroup(tempStr);
        iniFile.setValue("filename", "live2_ok.avi");    //端口
        ui->lineEdit->setText("live2_ok.avi");
        iniFile.endGroup();
    }
    else
    {
        QSettings iniFile(QCoreApplication::applicationDirPath()+"/test.ini", QSettings::IniFormat);
        iniFile.beginGroup("FileSet");
        QString value1=iniFile.value("filename").toString();
        qDebug()<<"filename:"<<value1;
        ui->lineEdit->setText(value1);
        iniFile.endGroup();
    }

    connect(mythr, SIGNAL(send_img(QImage)), this, SLOT(recv_img_show(QImage)));

}

void MainWindow::recv_img_show(QImage img)
{
    ui->label_2->showPic(img);
}

MainWindow::~MainWindow()
{
    QSettings iniFile(QCoreApplication::applicationDirPath()+"/test.ini", QSettings::IniFormat);

    iniFile.setIniCodec(QTextCodec::codecForName("GB2312"));    //解决读取value值的中文乱码问题

    QString name;
    if(ui->lineEdit->text().isEmpty())
    {
        name = "live2_ok.avi";
    }
    else
    {
        name = ui->lineEdit->text();
    }
    QString tempStr = QString("FileSet");
    iniFile.beginGroup(tempStr);
    iniFile.setValue("filename", name);    //端口
    iniFile.endGroup();

    delete ui;

}



void MainWindow::on_stopBtn_clicked()
{
    if(mythr->isFinished())
    {
//        mythr->thr_stop();
//        sleep(1);
        return;
    }
    mythr->thr_stop();
}

void MainWindow::on_startBtn_clicked()
{
    int index = ui->comboBox->currentIndex();
    qDebug()<<"index:"<<index;
    mythr->set_autoctr(ui->comboBox->currentIndex());
    if(mythr->isRunning())
    {
        mythr->thr_stop();
        mythr->wait();
        mythr->quit();
        sleep(1);
        mythr->start();
    }
    else
    {
        mythr->set_filename(ui->lineEdit->text());
        mythr->start();
    }
}

void MainWindow::on_openBtn_clicked()
{
    QString s = QFileDialog::getOpenFileName(
                    this, "选择要播放的文件",
                    "/",
                    "视频文件 (*.flv *.rmvb *.avi *.mp4);; 所有文件 (*.*);; ");
        qDebug() << "path=" << s;
        if (!s.isEmpty())
        {
            ui->lineEdit->setText(s);
            mythr->set_filename(s);
        }
}
